/*
 * Created by JFormDesigner on Sun Jun 09 10:36:56 CST 2024
 */

package org.study.bookmanagerclientbyswing.ui.StartView;

import java.awt.event.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.formdev.flatlaf.json.Json;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.study.bookmanagerclientbyswing.api.UserApi;
import org.study.bookmanagerclientbyswing.io.request.User.LoginReq;
import org.study.bookmanagerclientbyswing.io.request.User.RegisterReq;
import org.study.bookmanagerclientbyswing.io.response.RestBean;
import org.study.bookmanagerclientbyswing.ui.MainView.MainView;
import org.study.bookmanagerclientbyswing.utils.JsonUtil;
import org.study.bookmanagerclientbyswing.utils.JwtUtil;

import java.awt.*;
import java.util.Objects;
import javax.swing.*;

/**
 * @author 26482
 */
@Component
public class StartView extends JFrame {
    public StartView() {
        initComponents();
    }

    @Resource
    private UserApi userApi;

    @Resource
    private MainView mainView;

    private void ShowInfo(String msg) {
        infotext.setText(msg);
        info.setAlwaysOnTop(true);
        info.setVisible(true);
    }

    private void LoginMouseClicked(MouseEvent e)  {
        String userName = UserName.getText();
        String password = new String(Password.getPassword());
        RestBean restBean=userApi.login(new LoginReq(userName, password));
        if(restBean.status!=200){
            ShowInfo(restBean.message);
            return;
        }
        JwtUtil.getInstance().setJwtToken(JsonUtil.objToMap(restBean.getData()).get("token").toString());
        mainView.setVisible(true);
        this.setVisible(false);
        ShowInfo("登录成功");
        JwtUtil.getInstance().setUserName(userName);
    }

    private void RegisterMouseClicked(MouseEvent e)  {
        String userName = UserName2.getText();
        String password = new String(passwordField2.getPassword());
        String rePassword = new String(passwordField3.getPassword());
        if(!Objects.equals(password, rePassword)){
            ShowInfo("两次密码不一致,请重新输入");
            return;
        }
        RestBean restBean=userApi.register(new RegisterReq(userName, password));
        if(restBean.status!=200){
            ShowInfo(restBean.message);
            return;
        }
        JwtUtil.getInstance().setJwtToken(JsonUtil.objToMap(restBean.getData()).get("token").toString());
        mainView.setVisible(true);
        this.setVisible(false);
        ShowInfo("注册成功");
        JwtUtil.getInstance().setUserName(userName);
    }

    private void GoRegisterMouseClicked(MouseEvent e) {
        CardLayout cardLayout = (CardLayout) p.getLayout();//NB!在对象中的属性可以通过get获取再修改。。
        cardLayout.show(p, "CardRegister");
    }

    private void GoLoginMouseClicked(MouseEvent e) {
        CardLayout cardLayout = (CardLayout) p.getLayout();
        cardLayout.show(p, "CardLogin");
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        Main = new JPanel();
        p = new JPanel();
        plogin = new JPanel();
        label7 = new JLabel();
        label8 = new JLabel();
        UserName = new JTextField();
        Password = new JPasswordField();
        Login = new JButton();
        GoRegister = new JButton();
        pregister = new JPanel();
        label4 = new JLabel();
        label5 = new JLabel();
        UserName2 = new JTextField();
        passwordField2 = new JPasswordField();
        Register = new JButton();
        label6 = new JLabel();
        GoLogin = new JButton();
        passwordField3 = new JPasswordField();
        image = new JLabel();
        info = new JDialog();
        infotext = new JLabel();

        //======== this ========
        setTitle("\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf | \u5f00\u59cb");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== Main ========
        {
            Main.setLayout(new BoxLayout(Main, BoxLayout.X_AXIS));

            //======== p ========
            {
                p.setLayout(new CardLayout());

                //======== plogin ========
                {
                    plogin.setLayout(null);

                    //---- label7 ----
                    label7.setText("\u7528\u6237\u540d");
                    plogin.add(label7);
                    label7.setBounds(25, 35, label7.getPreferredSize().width, 20);

                    //---- label8 ----
                    label8.setText("\u5bc6\u7801");
                    plogin.add(label8);
                    label8.setBounds(30, 80, 40, 20);
                    plogin.add(UserName);
                    UserName.setBounds(90, 30, 120, UserName.getPreferredSize().height);
                    plogin.add(Password);
                    Password.setBounds(90, 75, 160, Password.getPreferredSize().height);

                    //---- Login ----
                    Login.setText("\u767b\u9646");
                    Login.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            LoginMouseClicked(e);
                        }
                    });
                    plogin.add(Login);
                    Login.setBounds(25, 120, 205, 40);

                    //---- GoRegister ----
                    GoRegister.setText("\u8fd8\u672a\u6ce8\u518c?\u70b9\u51fb\u6ce8\u518c");
                    GoRegister.setForeground(new Color(0x6666ff));
                    GoRegister.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            GoRegisterMouseClicked(e);
                        }
                    });
                    plogin.add(GoRegister);
                    GoRegister.setBounds(new Rectangle(new Point(50, 170), GoRegister.getPreferredSize()));

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < plogin.getComponentCount(); i++) {
                            Rectangle bounds = plogin.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = plogin.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        plogin.setMinimumSize(preferredSize);
                        plogin.setPreferredSize(preferredSize);
                    }
                }
                p.add(plogin, "CardLogin");

                //======== pregister ========
                {
                    pregister.setLayout(null);

                    //---- label4 ----
                    label4.setText("\u7528\u6237\u540d");
                    pregister.add(label4);
                    label4.setBounds(25, 35, label4.getPreferredSize().width, 20);

                    //---- label5 ----
                    label5.setText("\u5bc6\u7801");
                    pregister.add(label5);
                    label5.setBounds(30, 80, 40, 20);
                    pregister.add(UserName2);
                    UserName2.setBounds(90, 30, 120, UserName2.getPreferredSize().height);
                    pregister.add(passwordField2);
                    passwordField2.setBounds(90, 70, 160, passwordField2.getPreferredSize().height);

                    //---- Register ----
                    Register.setText("\u6ce8\u518c");
                    Register.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            RegisterMouseClicked(e);
                        }
                    });
                    pregister.add(Register);
                    Register.setBounds(25, 155, 205, 40);

                    //---- label6 ----
                    label6.setText("\u8bf7\u518d\u8f93\u5165\u5bc6\u7801");
                    pregister.add(label6);
                    label6.setBounds(5, 120, 95, 20);

                    //---- GoLogin ----
                    GoLogin.setText("\u56de\u5230\u767b\u9646");
                    GoLogin.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            GoLoginMouseClicked(e);
                        }
                    });
                    pregister.add(GoLogin);
                    GoLogin.setBounds(new Rectangle(new Point(240, 160), GoLogin.getPreferredSize()));
                    pregister.add(passwordField3);
                    passwordField3.setBounds(90, 115, 160, passwordField3.getPreferredSize().height);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < pregister.getComponentCount(); i++) {
                            Rectangle bounds = pregister.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = pregister.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        pregister.setMinimumSize(preferredSize);
                        pregister.setPreferredSize(preferredSize);
                    }
                }
                p.add(pregister, "CardRegister");
            }
            Main.add(p);

            //---- image ----
            image.setIcon(new ImageIcon("C:\\Users\\26482\\Desktop\\{E1977325-FB67-4885-A985-CA15456782A1}.png"));
            Main.add(image);
        }
        contentPane.add(Main);
        Main.setBounds(0, 0, 568, 229);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== info ========
        {
            info.setTitle("Info");
            info.setAlwaysOnTop(true);
            var infoContentPane = info.getContentPane();
            infoContentPane.setLayout(new BoxLayout(infoContentPane, BoxLayout.X_AXIS));

            //---- infotext ----
            infotext.setText("text");
            infoContentPane.add(infotext);
            info.pack();
            info.setLocationRelativeTo(info.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel Main;
    private JPanel p;
    private JPanel plogin;
    private JLabel label7;
    private JLabel label8;
    private JTextField UserName;
    private JPasswordField Password;
    private JButton Login;
    private JButton GoRegister;
    private JPanel pregister;
    private JLabel label4;
    private JLabel label5;
    private JTextField UserName2;
    private JPasswordField passwordField2;
    private JButton Register;
    private JLabel label6;
    private JButton GoLogin;
    private JPasswordField passwordField3;
    private JLabel image;
    private JDialog info;
    private JLabel infotext;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
